package com.example.universitylibraryback.service.impl;

import com.example.universitylibraryback.mapper.BookMapper;
import com.example.universitylibraryback.entity.Book;
import com.example.universitylibraryback.service.BookService;
import com.example.universitylibraryback.service.CacheService;
import com.example.universitylibraryback.dto.BookDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BookServiceImpl implements BookService {
    
    @Autowired
    private BookMapper bookMapper;
    
    @Autowired
    private CacheService cacheService;
    
    @Override
    public List<Book> findAllBooks() {
        return bookMapper.findAll();
    }
    
    @Override
    public Book findBookById(String bookId) {
        // 从缓存获取BookDTO
        BookDTO cachedBookDTO = cacheService.getCachedBookInfo(bookId);
        if (cachedBookDTO != null) {
            // 将DTO转换回实体
            Book book = new Book();
            BeanUtils.copyProperties(cachedBookDTO, book);
            return book;
        }
        
        // 缓存未命中，从数据库查询
        Book book = bookMapper.findById(bookId);
        if (book != null) {
            // 将实体转换为DTO并缓存
            BookDTO bookDTO = new BookDTO();
            BeanUtils.copyProperties(book, bookDTO);
            cacheService.cacheBookInfo(bookDTO);
        }
        return book;
    }
    
    @Override
    public Book findBookByIsbn(String isbn) {
        return bookMapper.findByIsbn(isbn);
    }
    
    @Override
    public List<Book> searchBooksByTitle(String title) {
        return bookMapper.findByTitle(title);
    }
    
    @Override
    public List<Book> searchBooksByAuthor(String author) {
        return bookMapper.findByAuthor(author);
    }
    
    @Override
    public List<Book> findAvailableBooks() {
        // 从缓存获取可借图书列表
        List<BookDTO> cachedBookDTOs = cacheService.getCachedAvailableBooks();
        if (cachedBookDTOs != null) {
            // 将DTO列表转换回实体列表
            List<Book> books = new ArrayList<>();
            for (BookDTO dto : cachedBookDTOs) {
                Book book = new Book();
                BeanUtils.copyProperties(dto, book);
                books.add(book);
            }
            return books;
        }
        
        // 缓存未命中，从数据库查询
        List<Book> books = bookMapper.findAvailableBooks();
        
        // 将实体列表转换为DTO列表并缓存
        List<BookDTO> bookDTOs = new ArrayList<>();
        for (Book book : books) {
            BookDTO dto = new BookDTO();
            BeanUtils.copyProperties(book, dto);
            bookDTOs.add(dto);
        }
        cacheService.cacheAvailableBooks(bookDTOs);
        
        return books;
    }
    
    @Override
    public boolean addBook(Book book) {
        boolean result = bookMapper.insert(book) > 0;
        if (result) {
            // 添加成功后清除相关缓存列表
            cacheService.clearBookRelatedCache();
            // 直接缓存新添加的图书
            BookDTO bookDTO = new BookDTO();
            BeanUtils.copyProperties(book, bookDTO);
            cacheService.cacheBookInfo(bookDTO);
        }
        return result;
    }
    
    @Override
    public boolean updateBook(Book book) {
        boolean result = bookMapper.update(book) > 0;
        if (result) {
            // 更新成功后清除相关缓存
            cacheService.removeBookCache(book.getBookId());
            cacheService.clearBookRelatedCache();
        }
        return result;
    }
    
    @Override
    public boolean deleteBook(String bookId) {
        Book book = bookMapper.findById(bookId);
        if (book == null || "已借".equals(book.getBookStatus())) {
            return false;
        }
        
        boolean result = bookMapper.delete(bookId) > 0;
        if (result) {
            // 删除成功后清除相关缓存
            cacheService.removeBookCache(bookId);
            cacheService.clearBookRelatedCache();
        }
        return result;
    }
    
    @Override
    public boolean updateBookStatus(String bookId, String status) {
        boolean result = bookMapper.updateBookStatus(bookId, status) > 0;
        if (result) {
            // 更新状态后清除相关缓存
            cacheService.removeBookCache(bookId);
            cacheService.clearBookRelatedCache();
        }
        return result;
    }
}